//
//  MedicalViewController.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/5/17.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "MedicalViewController.h"
#import "MedicalTableViewCell.h"
#import "CarShopTableViewCell.h"
#import "MedicalModel.h"
#import "TOWebViewController.h"

@interface MedicalViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    
    
    
}
//体验特惠信息
@property (weak, nonatomic) IBOutlet UIImageView *subTitleImageView;

@property (strong, nonatomic) NSMutableArray *modelAry;//datasource

@property (weak, nonatomic) IBOutlet UIImageView *headerImage;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerViewHeight;//deader高度

@property (weak, nonatomic) IBOutlet UITableView *medicalTV;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;

@property (weak, nonatomic) IBOutlet UIButton *topBtn;

@property (weak, nonatomic) IBOutlet UIButton *downBtn;

@end

@implementation MedicalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.medicalTV.dataSource = self;
    self.medicalTV.delegate = self;
    //隐藏分割线
    [self.medicalTV setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self showIndicator:@"正在加载数据，请稍候..."];
    
    [self getModelAryInfo];//请求数据
    
    [self screenAdaptation];//适配
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- viewWillAppear & disAppear
- (void)viewWillAppear:(BOOL)animated{
    
}

#pragma mark -- 屏幕适配
- (void)screenAdaptation{
    //Header 适配
    self.headerViewHeight.constant = MAIN_WIDTH / 375 * 200;
    
    switch (_VCType) {
        case MedicalVC:{
            
            self.topBtn.hidden = YES;
            
            [self.downBtn setBackgroundImage:[UIImage imageNamed:@"体检预约.png"] forState:(UIControlStateNormal)];
            
        }//医疗
            
            break;
        case CarShopVC:{
            
            [self.topBtn setBackgroundImage:[UIImage imageNamed:@"约试驾.png"] forState:(UIControlStateNormal)];
            
            [self.downBtn setBackgroundImage:[UIImage imageNamed:@"询底价.png"] forState:(UIControlStateNormal)];

        }
            break;
        default:
            break;
    }
    
    
}

#pragma mark -- 请求网络
- (NSString *)getVCTypesURL{
    
    switch (_VCType) {
        case MedicalVC:
            return @"19";
            break;
            
        default:
            return @"25";
            break;
    }
    
}

- (void)getModelAryInfo{
    
    __weak __typeof(self) weakSelf = self;
    NSString *str = [NSString stringWithFormat:@"%@/Api/test/index?tid=%@&id=%@",urlHeader,[self getVCTypesURL],self.urlId];
    NSLog(@"%@",str);
    [HTTPManager get:[NSString stringWithFormat:@"%@/Api/test/index?tid=%@&id=%@",urlHeader,[self getVCTypesURL],self.urlId] success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self.modelAry removeAllObjects];
        
        for (NSDictionary *aDic in responseObject[@"date"][@"date"]) {
            
            MedicalModel *model = [[MedicalModel alloc] init];
            [model setValuesForKeysWithDictionary:responseObject];
            [model setValuesForKeysWithDictionary:responseObject[@"date"]];
            [model setValuesForKeysWithDictionary:aDic];
            
            [self.modelAry addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.headerImage sd_setImageWithURL:[NSURL URLWithString:[self.modelAry[0] top_pic]] placeholderImage:[UIImage imageNamed:@"顶部占位图.png"]];
            
            [weakSelf updateTitle];
            
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [weakSelf hideIndicator];
    }];
    
}

#pragma mark -- 更新VC 标题 副标题
- (void)updateTitle{
    
    switch (_VCType) {
        case MedicalVC:{
            self.titleLabel.text = [self.modelAry[0] title];
            self.subTitleLabel.text = [self.modelAry[0] yhmsg];
            self.subTitleImageView.image = [UIImage imageNamed:@"体检特惠套餐.png"];

        }
            
            break;
            
        default:{
            self.titleLabel.text = [self.modelAry[0] name];
            self.subTitleLabel.text = [self.modelAry[0] title];
            self.subTitleImageView.image = [UIImage imageNamed:@"特惠套餐.png"];

        }
            break;
    }
    
    [self hideIndicator];
    [self.medicalTV reloadData];

    
}

#pragma mark -- TableViewDataSource & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (_VCType) {
        case MedicalVC:
            return MAIN_WIDTH / 375 * 152 + [NSString heightForString:[self.modelAry[indexPath.row] desc] fontSize:12 width:MAIN_WIDTH - 32] + 23;
            break;
            
        default:
            return MAIN_WIDTH / 375 * 152 + 10;
            break;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (_VCType) {
        case MedicalVC:{
            
            static NSString *cellId = @"MedicalTVC";
            MedicalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                //        cell = [[MedicalTableViewCell alloc] init];
                cell = [[[NSBundle mainBundle] loadNibNamed:@"MedicalTableViewCell" owner:self options:nil] lastObject];
            }
            
            cell.model = self.modelAry[indexPath.row];
            
            return cell;
            
        }
            
            break;
            
        default:{
            
            static NSString *cellId = @"CarShopCell";
            CarShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CarShopTableViewCell" owner:self options:nil] lastObject];
            }
            
            cell.model = self.modelAry[indexPath.row];
            
            return cell;
            
        }
            break;
    }
    

}

#pragma mark -- Navigation返回Btn Click
- (IBAction)navigationReturnBtn:(id)sender {

    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark -- 懒加载
- (NSMutableArray *)modelAry{
    if (_modelAry == nil) {
        _modelAry = [NSMutableArray new];
    }
    return _modelAry;
}

#pragma mark -- 侧边栏Btn
- (IBAction)topBtnClick:(id)sender {
    
    TOWebViewController *webViewController = [[TOWebViewController alloc] initWithURL:[NSURL URLWithString:[((MedicalModel *)self.modelAry[0]) url]]];
    self.hidesBottomBarWhenPushed = YES;
   
    [self.navigationController pushViewController:webViewController animated:YES];
    
}

- (IBAction)downBtnClick:(id)sender {
    
    TOWebViewController *webViewController = [[TOWebViewController alloc] initWithURL:[NSURL URLWithString:[((MedicalModel *)self.modelAry[0]) url]]];
     self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webViewController animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
